package per.hx.bs.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import per.hx.bs.util.HttpServletUtils;
import per.hx.bs.util.IPAddressUtil;
import per.hx.bs.util.SpringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @author HuXing
 * @date 2018/3/26  15:46
 */
@Aspect
@Component
public class BscLogAspect {
    /**
     * 保存日志到数据库的线程池
     */


    @Pointcut("@annotation(per.hx.bs.base.annotation.BsCLog)")
    public void bsClogPointCut() {
    }

    @Around("bsClogPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        // 执行方法
        Object result = point.proceed();
        // 执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        //异步保存日志
        // 获取request
        HttpServletRequest request = HttpServletUtils.getHttpServletRequest();
        //获取请求的ip
        String ip = IPAddressUtil.getIpAddress(request);
        //保存日志到数据库
        SaveLogTask saveLogTask = SpringUtils.getBean(SaveLogTask.class);
        saveLogTask.saveLog(point, time, ip);
        return result;
    }

}
